package com.ruoyi.web.controller.business;

import com.ruoyi.business.domain.RechargeOrder;
import com.ruoyi.business.service.BusinessService;
import com.ruoyi.business.service.RechargeOrderService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.domain.DictData;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.service.SysDictDataService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 话费充值订单Controller
 * 
 * @author xx
 * @date 2020-03-22
 */
@Controller
@RequestMapping("/business/order")
public class RechargeOrderController extends BaseController {
    private String prefix = "business/order";

    @Autowired
    private RechargeOrderService rechargeOrderService;
    @Autowired
    private SysDictDataService sysDictDataService;
    @Autowired
    private BusinessService businessService;

    @RequiresPermissions("business:order:view")
    @GetMapping()
    public String order() {
        return prefix + "/order";
    }

    /**
     * 查询话费充值订单列表
     */
    @RequiresPermissions("business:order:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(RechargeOrder rechargeOrder) {
        startPage();
        if(!SysUser.isAdmin(ShiroUtils.getUserId())) {
            rechargeOrder.setUserId(ShiroUtils.getUserId());
        }
        List<RechargeOrder> list = rechargeOrderService.selectRechargeOrderList(rechargeOrder);
        return getDataTable(list);
    }

    /**
     * 导出话费充值订单列表
     */
    @RequiresPermissions("business:order:export")
    @Log(title = "话费充值订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(RechargeOrder rechargeOrder) {
        List<RechargeOrder> list = rechargeOrderService.selectRechargeOrderList(rechargeOrder);
        ExcelUtil<RechargeOrder> util = new ExcelUtil<>(RechargeOrder.class);
        return util.exportExcel(list, "充值订单", sysDictDataService.selectAll(RechargeOrder.class));
    }

    /**
     * 新增话费充值订单
     */
    @GetMapping("/add")
    public String add(ModelMap mmap) {
        if (!SysUser.isAdmin(ShiroUtils.getUserId())) {
            // 方便前端thymeleaf模版，故转为字符串集合。
            mmap.put("rechargeChannels", businessService.selectRechargeChannelsByUserId(ShiroUtils.getUserId()).stream().map(integer -> integer + "").collect(Collectors.toList()));
        }
        return prefix + "/add";
    }

    /**
     * 新增保存话费充值订单
     */
    @RequiresPermissions("business:order:add")
    @Log(title = "话费充值订单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated RechargeOrder rechargeOrder) {
        rechargeOrder.setCreateBy(ShiroUtils.getLoginName());
        rechargeOrder.setUserId(ShiroUtils.getUserId());
        return toAjax(rechargeOrderService.insertRechargeOrder(rechargeOrder));
    }

    @RequiresPermissions("business:order:settle")
    @Log(title = "话费充值订单-修改结算状态", businessType = BusinessType.UPDATE)
    @PostMapping("/settle")
    @ResponseBody
    public AjaxResult settle(RechargeOrder rechargeOrder) {
        rechargeOrder.setUpdateBy(ShiroUtils.getLoginName());
        rechargeOrder.setUserId(ShiroUtils.getUserId());
        return toAjax(rechargeOrderService.settle(rechargeOrder));
    }

    /**
     * 修改话费充值订单
     */
    /*@GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        RechargeOrder rechargeOrder = rechargeOrderService.selectRechargeOrderById(id);
        mmap.put("rechargeOrder", rechargeOrder);
        return prefix + "/edit";
    }*/

    /**
     * 修改保存话费充值订单
     */
    /*@RequiresPermissions("business:order:edit")
    @Log(title = "话费充值订单", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(RechargeOrder rechargeOrder) {
        return toAjax(rechargeOrderService.updateRechargeOrder(rechargeOrder));
    }*/

    /**
     * 删除话费充值订单
     */
    /*@RequiresPermissions("business:order:remove")
    @Log(title = "话费充值订单", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(rechargeOrderService.deleteRechargeOrderByIds(ids));
    }*/

    @PostMapping("notify")
    @ResponseBody
    public String doNotify(HttpServletRequest request) {
        Map<String, String[]> params = request.getParameterMap();
        rechargeOrderService.doNotify(params);
        return "success";
    }

    @Log(title = "话费充值订单", businessType = BusinessType.IMPORT)
    @RequiresPermissions("business:order:import")
    @PostMapping("importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<RechargeOrder> util = new ExcelUtil<>(RechargeOrder.class);
        List<RechargeOrder> orderList = util.importExcel(file.getInputStream(), sysDictDataService.selectAll(RechargeOrder.class));
        String operName = ShiroUtils.getSysUser().getLoginName();
        Long operId = ShiroUtils.getUserId();
        String message = rechargeOrderService.importOrder(orderList, operId, operName);
        return AjaxResult.success(message);
    }

    @RequiresPermissions("business:order:import")
    @GetMapping("importTemplate")
    @ResponseBody
    public AjaxResult importTemplate() {
        ExcelUtil<RechargeOrder> util = new ExcelUtil<>(RechargeOrder.class);
        // excel中的充值通道只能看到自己开通过的，所以这里需要过滤一下
        List<Integer> rechargeChannels = businessService.selectRechargeChannelsByUserId(ShiroUtils.getUserId());
        Map<String, List<DictData>> dictMap = sysDictDataService.selectAll(RechargeOrder.class);
        List<DictData> dataList = dictMap.get("sd_recharge_channel");
        dictMap.put("sd_recharge_channel", dataList.stream().filter(dictData -> rechargeChannels.contains(Integer.valueOf(dictData.getDictValue()))).collect(Collectors.toList()));
        return util.importTemplateExcel("批量充值模版", dictMap);
    }
}
